package com.example.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        Button btnBack = findViewById(R.id.btn_back);
        // 匿名内部类 适用于有唯一操作的按钮
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        Button btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new MyClickListener());

        Button btn3 = findViewById(R.id.btn3);
        btn3.setOnClickListener(this::onClick);


    }

    public void myClick(View v) {

        if (v.getId() == R.id.btn4) {
            Log.e("TAG","通过 btn4 xml绑定");
        } else if (v.getId() == R.id.btn5) {
            Log.e("TAG","通过 btn5 xml绑定");
        }
    }


    @Override
    public void onClick(View view) {
        Log.e("TAG","用本类实现OnClickListener");
    }


    class MyClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            Log.e("TAG","内部类监听器 按钮");
        }
    }


}